﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace WebRPG
{
    public partial class Login : System.Web.UI.Page
    {
        public const string serverfilepath_Accounts = "D:\\ServerBD\\Accounts.bdf";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            // Check account
            string login = Request.Form["inputLogin"].ToString();
            string password = Request.Form["inputPassword"].ToString();

            if (login == "" || password == "")
            {
                Label1.ForeColor = System.Drawing.Color.Red;
                Label1.Text = "Не введен логин или пароль.";
                return;
            }

            if (login.Contains(';') || password.Contains(';'))
            {
                Label1.ForeColor = System.Drawing.Color.Red;
                Label1.Text = "Логин или пароль содержит символ ';'.";
                return;
            }

            // Read
            using (StreamReader sr = new StreamReader(serverfilepath_Accounts))
            {
                for (; sr.EndOfStream != true; )
                {
                    string line = sr.ReadLine();
                    if (line == login + ";" + password)
                    {
                        Response.Redirect("Main.aspx");
                        return;
                    }
                }
            }

            // Write
            StreamWriter sw = new StreamWriter(serverfilepath_Accounts, true);
            sw.WriteLine(login + ";" + password);
            sw.Close();
            Label1.ForeColor = System.Drawing.Color.Green;
            Label1.Text = "Создана запись.";
        }
    }
}